/* eslint-disable no-async-promise-executor */
// eslint-disable-next-line no-unused-vars
const model = require("../models/model")
const MyGoodsListType = model.goodsdetailsType.MyGoodsListType
const MyGoods = model.goodsdetails.MyGoods //获取模型
const MyGoodsSku = model.goodsdetailsSku.MyGoodsListSku
const MyGoodsList = model.goodsdetailsList.MyGoodsList
exports.modifySkuType = async(ctx) => {
  const { id, goodsId, skuId, name, imgId } = ctx.request.body
  let data = {
    id: id,
    skuId: skuId,
    parentId: goodsId,
    name: name,
    previewImgUrl: imgId,
    imgUrl: imgId
  }
  for (const prop in data) {
    if (data[prop] == undefined || data[prop] == null) {
      delete data[prop]
    }
  }

  function edit({id, skuId, parentId, name, previewImgUrl, imgUrl}) {
    return new Promise(async(resolve) => {
      if (!id && !skuId && !parentId && !name && !previewImgUrl && !imgUrl) {
        resolve(-1)
        return
      }
      const res = await MyGoodsListType.update(data, {
        where: {
          id: id
        }
      })
      if (res[0] == 1) {
        resolve(res)
      }
    })
  }

  function add({skuId, parentId, name, previewImgUrl, imgUrl}) {
    return new Promise(async(resolve) => {
      if (!skuId && !parentId && !name && !previewImgUrl && !imgUrl) {
        resolve(-1)
        return
      }
      // 增加规格类目是 同时要判断商品表的sku是否已经存有了相同字段
      const goods_res  = new Promise(async(resolve) => {
        const res = await MyGoods.findOne({ where: { id: parentId } })
        if (res) resolve(res)
      })
      const sku_res = new Promise(async(resolve) => {
        const res = await MyGoodsSku.findOne({ where: { id: skuId } })
        if (res) resolve(res)
      })
      const promise_all = await Promise.all([goods_res, sku_res])
      if (promise_all) {

        if (!promise_all[0].sku.includes(promise_all[1].k_s)) { // 是否有对应的字段
          // 没有的话添加对应字段
          let skuTextList = promise_all[0].sku.split(',').filter(item => item !== '')
          skuTextList.push(promise_all[1].k_s)
          // 更新goods表 sku的值
          const goods_update = await MyGoods.update({ sku: skuTextList.join(','), none_sku: 0  }, {
            where: { id: parentId }
          })
          if (goods_update[0] == 1) { // 在创建该规格类目
            const res = await MyGoodsListType.create(data)
            if (res) {
              resolve(res)
            }
          }
          // console.log('没有')
        } else {
          // console.log('有')
          const res = await MyGoodsListType.create(data)
          if (res) {
            resolve(res)
            // 更新goods表 sku的值
            MyGoods.update({ none_sku: 0  }, {
              where: { id: parentId }
            })
          }
        }
      }
    })
  }
  let res
  if (id) {
    res = await edit(data)
  } else {
    res = await add(data)
  }
  if (res) {
    ctx.success()
  } else {
    ctx.fail('参数错误')
  }
}

// 无效接口
// 无效接口
// 无效接口
// 无效接口
exports.deleteSkuType = async(ctx) => {
  const { id, key } = ctx.request.query
  if (!id || !key) {
    ctx.fail('参数错误')
    return
  }
  let data = {} // 用于list表的查找
  data[key] = id
  function allFn() {
    return new Promise(async(resolve) => {
      // 现在规格列表List查找是否有使用到（有的话不能删除）
      const res = await MyGoodsList.findOne({ where: data })
      if (res) {
        resolve('该规格正在被使用！！请先清除规格列表再进行操作')
      } else {
        const delete_res = await MyGoodsListType.destroy({
          where: { id: id }
        })
        if (delete_res) resolve(0)
      }
    })
  }
  const res = await allFn()
  if (res === 0) {
    ctx.success('', '删除成功')
  } else {
    ctx.fail(res)
  }
}
